create or replace function deleteDepartmentType(
	out xml, inout xml
	)
as
$$
	declare
		-- alias is name for stored procedure parameters
		executeResultData	alias for $1;
		departmentTypeData	alias for $2;
		
		-- variable for node data
		vIdDepartmentType integer := null;	-- department type id in DB
		
		-- node path
		vIdNode	varchar(100) := '/departmenttype/@id';
	
	begin
		-- get data from xml
		vIdDepartmentType := cast(extract_value(vIdNode, departmentTypeData) as integer);
		
		-- insert data into trace table
		insert into DepartmentTypeTrace(idDepartmentType, nameDepartmentType, dateFrom)
			select depe.idDepartmentType, depe.nameDepartmentType, depe.dateFrom from DepartmentType depe
				where
					depe.idDepartmentType = vIdDepartmentType;
					
		-- delete row from table
		delete from DepartmentType depe
			where
				depe.idDepartmentType = vIdDepartmentType;
		
		executeResultData := statusCall(successcode(), deleteText());
	end;
$$
language plpgsql VOLATILE;